package com.tarena.luban.worker.server.repository.impl;

import com.tarena.luban.worker.server.bo.WorkerCategoryBO;
import com.tarena.luban.worker.server.dao.WorkerCategoryDao;
import com.tarena.luban.worker.server.repository.WorkerCategoryRepository;
import com.tarena.luban.worker.server.repository.converter.WorkerCategoryConverter;
import com.tarena.tp.luban.worker.po.WorkerCategory;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCategoryParam;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCreateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class WorkerCategoryRepoImpl implements WorkerCategoryRepository {
    @Autowired
    private WorkerCategoryDao workerCategoryDao;
    @Override
    public void deleteCategory(Long userId) {
        workerCategoryDao.delete(userId);
    }
    @Autowired
    private WorkerCategoryConverter workerCategoryConverter;
    @Override
    public void saveWorkerCategory(WorkerCreateParam workerCreateParam) {
        //从param参数中获取 category的新增参数 List
        List<WorkerCategoryParam> workerCategoryParams = workerCreateParam.getWorkerCategoryParams();
        //补齐userId
        for (WorkerCategoryParam param : workerCategoryParams) {
            param.setUserId(workerCreateParam.getUserId());
        }
        //转化成entity的list
        List<WorkerCategory> workerCategories =
                workerCategoryConverter.params2pos(workerCategoryParams);
        //进行批量新增
        workerCategoryDao.insertCategories(workerCategories);
        //假设list元素有2个 insert into worker_category(字段) values (第一个元素),(第二个元素)
    }

    @Override
    public List<WorkerCategoryBO> getCatsByUserId(Long userId) {
        List<WorkerCategory> categories=workerCategoryDao.selectByUserId(userId);
        return workerCategoryConverter.poList2BoList(categories);
    }
}
